package org.example.controller;

import org.example.data.Result;
import org.example.data.Room;
import org.example.data.RoomIn;
import org.example.tool.MySQL;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminController {

    public Result<Integer> addRoom(Room room){
        Integer n = null;
        try {
            n = insertRoom(room);
            if (n > 0) {
                return new Result<>(true, "房间添加成功", n);
            } else {
                return new Result<>(false, "房间添加失败", null);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public Result<Boolean> updateRoom(Room room, Integer id) throws SQLException {
        if (!room.getName().isEmpty()){
            Integer n = updateRoom("name",room.getName(),id);
            if (n!=1){
                return new Result<>(false,"修改房间名称错误",null);
            }
        }
        if (!room.getFloor().isEmpty()){
            Integer n = updateRoom("FLOOR",room.getFloor(),id);
            if (n!=1){
                return new Result<>(false,"修改房间楼层错误",null);
            }
        }
        if (!"".equals(room.getPrice())){
            Integer n = updateRoom(room.getPrice(),id);
            if (n!=1){
                return new Result<>(false,"修改房间价格错误",null);
            }
        }
        return new Result<>(true,"修改成功",null);
    }

    /**
     * 办理入住并返回Result
     * @param roomIn
     * @return Result<RoomIn>
     * @throws SQLException
     */
    public Result<RoomIn> checkIn(RoomIn roomIn) throws SQLException {
        Integer n = insertRoomIn(roomIn);
        if (n > 0) {
            updateRoomStatus(roomIn.getRoomId(),"入住中");
            return new Result<>(true, "入住办理成功", roomIn);
        } else {
            return new Result<>(false, "入住办理失败", null);
        }
    }

    /**
     * 办理退房
     * @param roomIn
     * @return
     * @throws SQLException
     */
    public Result<Boolean> checkOut(RoomIn roomIn) throws SQLException {
        Integer n = updateCheckRoom(roomIn);
        if (n == 1){
            updateRoomStatus(roomIn.getRoomId(),"空闲");
            updateCheckRoom(roomIn.getRoomId());
            return new Result<>(true, "退房办理成功", true);
        }
        else return new Result<>(false,"退房失败",false);
    }

    public Result<List<Room>> viewRooms() throws SQLException {
        List<Room> roomList = queryRoomList();
        if (!roomList.isEmpty()) {
            return new Result<>(true, "房间信息查询成功", roomList);
        } else {
            return new Result<>(false, "没有找到房间信息", null);
        }
    }
    public Result<List<RoomIn>> viewRoomIns()throws SQLException {
        List<RoomIn> roomList = queryRoomInList();
        if (!roomList.isEmpty()) {
            return new Result<>(true, "房间信息查询成功", roomList);
        } else {
            return new Result<>(false, "没有找到房间信息", null);
        }
    }


    /**
     * 添加房间
     * @param room
     * @return
     * @throws SQLException
     */
    private Integer insertRoom(Room room) throws SQLException {
        MySQL mySQL = new MySQL();
        String sql = "insert into room (name,status,floor,price) values (?, '空闲', ?, ?)";
        PreparedStatement statement = mySQL.getStatement(sql);
        statement.setString(1,room.getName());
        statement.setString(2,room.getFloor());
        statement.setDouble(3,room.getPrice());
        int n = statement.executeUpdate(); // 执行插入操作
        statement.close();
        mySQL.close();
        return n;
    }

    /**
     * 修改房间基本信息
     * @param type
     * @param data
     * @param id
     * @return
     * @throws SQLException
     */
    private Integer updateRoom(String type,String data,Integer id) throws SQLException {
        MySQL mySQL = new MySQL();
        String sql = "update room set " + type + " = ? where id = " + id;
        PreparedStatement statement = mySQL.getStatement(sql);
        statement.setString(1, data);
        int n = statement.executeUpdate(); // 执行更新操作
        statement.close();
        mySQL.close();
        return n;
    }

    /**
     * 修改房间价格
     * @param data
     * @param id
     * @return
     * @throws SQLException
     */
    private Integer updateRoom(Double data,Integer id) throws SQLException {
        MySQL mySQL = new MySQL();
        String sql = "update room set price = ? where id = " + id;
        PreparedStatement statement = mySQL.getStatement(sql);
        statement.setDouble(1, data);
        int n = statement.executeUpdate(); // 执行更新操作
        statement.close();
        mySQL.close();
        return n;
    }

    /**
     * 添加入住信息
     * @param room
     * @return
     * @throws SQLException
     */
    private Integer insertRoomIn(RoomIn room) throws SQLException {
        MySQL mySQL = new MySQL();
        String sql = "insert into roomin (roomid,userid,starttime,endtime) values (?, ?, ?, ?)";
        PreparedStatement statement = mySQL.getStatement(sql);
        statement.setInt(1,room.getRoomId());
        statement.setInt(2,room.getUserId());
        statement.setString(3,room.getStartTime());
        statement.setString(4,room.getEndTime());
        int n = statement.executeUpdate(); // 执行插入操作
        statement.close();
        mySQL.close();
        return n;
    }
    /**
     * 修改房间状态
     */
    private Integer updateRoomStatus(Integer id,String status) throws SQLException {
        MySQL mySQL = new MySQL();
        String sql = "update room set status = ? where id = " + id;
        PreparedStatement statement = mySQL.getStatement(sql);
        statement.setString(1, status);
        int n = statement.executeUpdate(); // 执行更新操作
        statement.close();
        mySQL.close();
        return n;
    }
    /**
     * 修改退房时间
     */
    private Integer updateCheckRoom(RoomIn room) throws SQLException {
        MySQL mySQL = new MySQL();
        System.out.println(room);
        String sql = "update roomin set checktime = NOW() where roomid = " + room.getRoomId();
        PreparedStatement statement = mySQL.getStatement(sql);
        int n = statement.executeUpdate(); // 执行更新操作
        statement.close();
        mySQL.close();
        return n;
    }
    private Integer updateCheckRoom(Integer id) throws SQLException {
        MySQL mySQL = new MySQL();
        String sql = "delete from roomin where roomid = " + id;
        PreparedStatement statement = mySQL.getStatement(sql);
        int n = statement.executeUpdate(); // 执行更新操作
        statement.close();
        mySQL.close();
        return n;
    }
    /**
     * 查询住房信息列表
     * @return 房间入住信息列表
     * @throws SQLException
     */
    public List<RoomIn> queryRoomInList() throws SQLException {
        List<RoomIn> roomInList = new ArrayList<>();
        MySQL mySQL = new MySQL();
        String sql = "SELECT * FROM roomin";
        PreparedStatement statement = mySQL.getStatement(sql);
        ResultSet resultSet = statement.executeQuery();

        while (resultSet.next()) {
            RoomIn roomIn = new RoomIn();
            roomIn.setId(resultSet.getInt("id"));
            roomIn.setRoomId(resultSet.getInt("roomid"));
            roomIn.setUserId(resultSet.getInt("userid"));
            roomIn.setStartTime(resultSet.getString("starttime"));
            roomIn.setEndTime(resultSet.getString("endtime"));
            // 假设checktime字段存在
            roomIn.setCheckTime(resultSet.getString("checktime"));
            roomInList.add(roomIn);
        }

        resultSet.close();
        statement.close();
        mySQL.close();
        return roomInList;
    }

    /**
     * 查询房间信息列表
     * @return 房间信息列表
     * @throws SQLException
     */
    public List<Room> queryRoomList() throws SQLException {
        List<Room> roomList = new ArrayList<>();
        MySQL mySQL = new MySQL();
        String sql = "SELECT * FROM room";
        PreparedStatement statement = mySQL.getStatement(sql);
        ResultSet resultSet = statement.executeQuery();
        while (resultSet.next()) {
            Room room = new Room();
            room.setId(resultSet.getInt("id"));
            room.setName(resultSet.getString("name"));
            room.setStatus(resultSet.getString("status"));
            room.setFloor(resultSet.getString("floor"));
            room.setPrice(resultSet.getDouble("price"));
            roomList.add(room);
        }
        resultSet.close();
        statement.close();
        mySQL.close();
        return roomList;
    }
}
